package com.vote.www.controller;

import com.vote.www.core.page.Pagination;
import com.vote.www.domain.Czjrecommendpic;
import com.vote.www.domain.vo.WeixinInfo;
import com.vote.www.service.RecommendpicService;
import com.vote.www.service.WeixininfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.Map;

/**
 * 弹出推荐图片
 * Created by 汤伟杰 on 2017/5/15.
 */

@Controller
    @RequestMapping("/recommendpic")
public class RecommendpicController extends BaseController{
    private static final Logger log = LoggerFactory.getLogger(CzjvoteorderController.class);
    @Autowired
    private RecommendpicService recommendpicService;
    @Autowired
    private WeixininfoService weixininfoService;

    /**
     * 列表
     * @param pageNo
     * @param pageSize
     * @param q
     * @param req
     * @param resp
     * @param model
     * @return
     */
    @RequestMapping({"list.do", ""})
    public String list(Integer pageNo, Integer pageSize, String q,
                       HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model) {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        Pagination pagination = recommendpicService.list(pageNo.intValue(), pageSize.intValue(), q, model);
        model.addAttribute("pagination", pagination);
        model.addAttribute("q", q);
        return "recommendpicList";
    }

    /**
     * 详细
     * @param id
     * @param req
     * @param resp
     * @param model
     * @return
     */
    @RequestMapping("detail.do")
    public String detail(@RequestParam(required = true) String id, HttpServletRequest req, HttpServletResponse resp,
                         ModelMap model) {
        try {
            model.addAttribute("msg",req.getParameter("msg"));
            recommendpicService.getDetail(id, model);

        } catch (Exception e) {
            e.printStackTrace();
            String param = "";
            Enumeration<String> enum2 = req.getParameterNames();
            if (enum2.hasMoreElements()) {
                param += "?";
            }
            while (enum2.hasMoreElements()) {
                param += enum2.nextElement() + "&";
            }
            param = param.substring(0, param.lastIndexOf("&"));
            model.addAttribute("msg", req.getRequestURI() + param + ":" + e.getMessage());
            return "error";
        }
        return "recommendpicDetail";
    }

    /**
     * 保存
     * @param czjrecommendpic
     * @param multipartFiles
     * @param req
     * @param resp
     * @param model
     * @return
     */
    @RequestMapping("save.do")
    public String save(Czjrecommendpic czjrecommendpic,
                     @RequestParam(value = "file", required = false) MultipartFile multipartFiles,
                     HttpServletRequest req, HttpServletResponse resp, ModelMap model) {
//        System.out.println("1");
        try {
            recommendpicService.saveRecommendpic(czjrecommendpic,multipartFiles,req);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg", "保存失败！详情："+e.getMessage());
        }
        return "redirect:detail.do?id="+czjrecommendpic.getRecommendpicId();

    }

    /**
     * 调用显示或隐藏弹出框，json返回
     * @param code
     * @param req
     * @param resp
     * @param model
     */
    @RequestMapping("getRecommendpic.do")
    public void getRecommendpic(@RequestParam() String code,
                                HttpServletRequest req, HttpServletResponse resp,
                                ModelMap model){
        try {

            WeixinInfo weixininfo = recommendpicService.getWeixinUserinfo(code);//得到微信用户信息
//            WeixinInfo weixininfo = weixininfoService.getWeixinUserInfoByOpenid(code);//测试用 openid
            Map<String,Object> map = recommendpicService.getByCode(weixininfo,req);
            jsonResult(1,"","",map,resp);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("#log error:",e);
            jsonResult(0,"99",e.getMessage(),"",resp);
        }
    }

    /**
     * 默认模板设置
     * @param req
     * @param resp
     * @param model
     */
    @RequestMapping("template.do")
    public String template(HttpServletRequest req, HttpServletResponse resp,
                                ModelMap model){
        model.addAttribute("msg", req.getParameter("msg"));
        Map<String,Object> map = recommendpicService.getTemplate();
        model.addAttribute("entity",map);
        return "recommendpictemplate";
    }

    /**
     * 保存默认模板
     * @param req
     * @param resp
     * @param model
     * @return
     */
    @RequestMapping("saveTemplate.do")
    public String saveTemplate(String url,String validTime,String title,String interval,String display,
                               @RequestParam(value = "file", required = false) MultipartFile multipartFiles,
                               HttpServletRequest req, HttpServletResponse resp,
                           ModelMap model){
        String msg = "";
        try {
            req.getParameterNames();
            recommendpicService.saveTemplate(url,validTime,title,interval,display,multipartFiles,req);
        } catch (Exception e) {
            e.printStackTrace();
            msg = "?msg=保存默认模板失败！详情：" + e.getMessage();
        }
        return "redirect:template.do"+msg;
    }
}
